﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.SalesManagement
{
    public partial class Price :BasePage
    {
       
        private static IPricePolicyBLL policyBLL = IocContainer.Instance.Resolve<IPricePolicyBLL>();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            SystemOverTime();
            if (!Page.IsPostBack)
            {
                // 按钮权限控制
                ControlAuthority(new List<WebControl> { btnAdd,btnUpdate,btnDelete,btnSearch }, 11);
            }
        }


        [WebMethod]
        public static Boolean DeletePolicy(int policyId)
        {
            Boolean result = false;
            try
            {
                policyBLL.Delete(policyId);
                result = true;
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }
            return result;

        }

        [WebMethod]
        public static string GetPolicies(string name, string code,string orderField, string orderValue, int pageIndex, int pagePerSize)
        {
            string strResult;
            //IPricePolicyBLL policyBLL = IocContainer.Instance.Resolve<IPricePolicyBLL>();
            StringBuilder strWhere = new StringBuilder(" where 1=1 ");
            if (!string.IsNullOrEmpty(name))
            {
                strWhere.Append(" and PolicyName like '%" + name + "%'");
            }
            if (!string.IsNullOrEmpty(code))
            {
                strWhere.Append(" and PolicyCode like '%" + code + "%'");
            }

            StringBuilder strOrderBy = new StringBuilder(" Order by ");
            strOrderBy.Append(!string.IsNullOrEmpty(orderField) ? " " + orderField : "  PolicyName  ");
            strOrderBy.Append(!string.IsNullOrEmpty(orderValue) ? " " + orderValue : "  asc ");

            int totalRecords = 0;
            int totalPages = 0;
            DataTable dtProducts = policyBLL.GetPoliciesByPage(strWhere.ToString(), strOrderBy.ToString(), pageIndex, pagePerSize, out totalRecords,
                                           out totalPages);

            strResult = SerializationHelper.DataTable2Json(dtProducts);

            return "{\"totalRecords\":\"" + totalRecords.ToString() + "\" ,\"totalPages\":\"" + totalPages.ToString() + "\",\"rows\":" +
                   strResult + " }";

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

        }

        protected void btnhidDelete_Click(object sender, EventArgs e)
        {

        }
    }
}
